package dao.impl;



import java.util.Iterator;
import java.util.List;



import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import model.Users;
import dao.DaoException;
import dao.UserDao;

public class UserDaoImpl extends Dao implements UserDao{

	@Override
	public void createUser(Users user) {
		hibernateTemplate.saveOrUpdate(user);
		
	}

	@Override
	public Users getUserByUsername(String user) throws DaoException {
		
		 
		 
		DetachedCriteria crit=DetachedCriteria.forClass(Users.class).add(Restrictions.eq("user",user));
        //@//SuppressWarnings("rawtypes")
		List results = hibernateTemplate.findByCriteria(crit);
		
       
        if (results.size() == 0) {
            throw new DaoException("No user found with the specified username!");
        }
        if (results.size() > 1) {
            throw new DaoException("Multiple users found with the specified username!");
        }
        return (Users) results.get(0);
	}

}
